﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DomMgmtSystem.View
{
    public partial class OutRegister : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal TB_HSSJTG_ID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["hssjtgID"]))
                    return Convert.ToDecimal(Request["hssjtgID"]);
                return 0;
            }
        }
        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }

        }
        /// <summary>
        /// 是否是归还
        /// </summary>
        public bool IsGH
        {
            get
            {
                return this.GetRequestStr("isgh") != string.Empty;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBindPage();

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();imghide();</script>");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtCSQBH.Text.Trim() != "")
            {
                Model.TB_YGSJTG model = new Model.TB_YGSJTG
                {
                    CTGBH = txtCTGBH.Text.Trim(),
                    NTGLX = Convert.ToDecimal(ddlNTGLX.SelectedValue),
                    CXMBH = txtCSQBH.Text.Trim(),
                    CJSDW = txtCJSDW.Text.Trim(),
                    CJSRY = txtCJSRY.Text.Trim(),
                    DTTGRQ = Convert.ToDateTime(txtDTTGRQ.Text.Trim()),
                    CWXLX = txtWXLX.Text.Trim()
                };

                if (ddlNTGLX.SelectedItem.Value == "2")
                {
                    //model.CNR = "";
                    //model.NHPS = 0;
                    //model.FDSXPF = 0;
                    //model.FSJL = 0;
                    //model.FMJ = 0;
                    //model.FSFBZ = 0;
                    //model.FYFJE = 0;
                    //model.FSHJE = 0;
                    //model.CTGDW = "";
                    //model.CTGRY = "";
                    //model.CJSDW = "";
                    //model.CJSRY = "";
                }
                else
                {
                    model.CWXLX = txtWXLX.Text.Trim();
                    model.CNR = txtCNR.Text.Trim();
                    model.NJS = Convert.ToDecimal(txtJS.Text.Trim());
                    model.FDSXPF = Convert.ToDecimal(txtFDSXPF.Text.Trim());
                    model.FSJL = Convert.ToDecimal(txtFSJL.Text.Trim());
                    model.FMJ = Convert.ToDecimal(txtFMJ.Text.Trim());
                    model.FSFBZ = Convert.ToDecimal(txtFSFBZ.Text.Trim());
                    model.FYFJE = Convert.ToDecimal(txtFYFJE.Text.Trim());
                    model.FSHJE = Convert.ToDecimal(txtFSHJE.Text.Trim());
                    model.CTGDW = txtCTGDW.Text.Trim();
                    model.CTGRY = txtCTGRY.Text.Trim();
                    model.CJSDW = txtCJSDW.Text.Trim();
                    model.CJSRY = txtCJSRY.Text.Trim();
                    model.CBZ = txtCBZ.Text.Trim();
                }
                if (TB_HSSJTG_ID != 0)
                {
                    model.NID = TB_HSSJTG_ID;
                    DataAccess.TB_YGSJTG.Update(model);
                }
                else
                    DataAccess.TB_YGSJTG.Add(model);

                if (TB_HSSJTG_ID != 0)
                    this.RedirectAfterInfo("编辑成功！", "OutStatistics.aspx");
                else
                    this.RedirectConfirmInfo("新增出库成功！是否继续新增？", "OutRegister.aspx", "OutStatistics.aspx");
            }
        }

        private void DataBindPage()
        {
            txtDTTGRQ.Text = DateTime.Now.ToString("yyyy-MM-dd");
            lbTitle.Text = "新增遥感出库信息";
            if (TB_HSSJTG_ID != 0)
            {
                Model.TB_YGSJTG model = DataAccess.TB_YGSJTG.GetModel(TB_HSSJTG_ID);
                txtCTGBH.Text = model.CTGBH;
                ddlNTGLX.SelectedValue = string.Format("{0:d}", Convert.ToInt32(model.NTGLX.Value));
                txtJS.Text = model.NJS.ToString();
                txtWXLX.Text = model.CWXLX.ToString();
                txtCSQBH.Text = model.CXMBH;
                txtCNR.Text = model.CNR;
                txtFDSXPF.Text = string.Format("{0:f}", model.FDSXPF.Value);
                txtFSJL.Text = string.Format("{0:f}", model.FSJL.Value);
                txtFMJ.Text = string.Format("{0:f}", model.FMJ.Value);
                txtFSFBZ.Text = string.Format("{0:f}", model.FSFBZ.Value);
                txtFYFJE.Text = string.Format("{0:f}", model.FYFJE.Value);
                txtFSHJE.Text = string.Format("{0:f}", model.FSHJE.Value);
                txtCTGDW.Text = model.CTGDW;
                txtCTGRY.Text = model.CTGRY;
                txtCJSDW.Text = model.CJSDW;
                txtCJSRY.Text = model.CJSRY;
                txtDTTGRQ.Text = string.Format("{0:yyyy-MM-dd}", model.DTTGRQ.Value);
                txtCBZ.Text = model.CBZ;
                txtDTGHRQ.Text = string.Format("{0:yyyy-MM-dd}", model.DTGHRQ.Value);
                ddlNTGLX.Enabled = false;
                txtCSQBH.ReadOnly = true;
                btnBack.Visible = true; txtCSQBH.Attributes.Remove("onblur");
                if (IsGH)
                {
                    lbTitle.Text = "归还编辑";
                    txtDTGHRQ.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    btnSave.Attributes.Add("ishow", "true");
                }
                else
                    lbTitle.Text = "编辑遥感出库信息";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("OutStatistics.aspx");
        }
    }
}